use "C:\Users\jkbrueck\Documents\STATA\Fuel 4\US data\postest1_type.dta", clear

merge m:1 type using "C:\Users\jkbrueck\Documents\STATA\Fuel 4\US data\mean lifespans.dta"

keep if _merge == 3
drop _merge

gen remain_lf = lifespan - age

*alternate cutoff for scrappage probability

gen noscrap = 0
*replace noscrap = 1 if prob < 0.5
replace noscrap = 1 if prob < 0.25
tab noscrap

keep if noscrap == 1

keep mkt_value airline scrap eff type prob fitvalue age remain_lf lifespan retyr

keep if remain_lf > 0

merge m:1 type airline retyr using "C:\Users\jkbrueck\Documents\STATA\Fuel 4\US data\fuel usage.dta"

replace gallons = 4.51e+07 if type == "767-300"
gen x = 0
replace x = 1 if type == "767-300"

keep if _merge == 3 | x == 1
drop _merge x

gen kgco2 = gallons * 9.75
gen mtco2 = kgco2/1000
gen co2_emis = mtco2

*alternate remaining life or discount rate

*gen own_emis = co2_emis * (1 - exp(-.05*remain_lf))/.05
*gen own_emis = co2_emis * (1 - exp(-.07*remain_lf))/.07
gen own_emis = co2_emis * (1 - exp(-.05*remain_lf/2))/.05
gen clunkers_pmt = -1000000 * fitvalue

summarize scrap

save "C:\Users\jkbrueck\Documents\STATA\Fuel 4\US data\clunkers1.dta", replace

drop if 40 * own_emis/clunkers_pmt < 1

gen ct = 1 

*collapse (mean) remain_lf gallons eff mkt_value own_emis fitvalue clunkers_pmt (sum) ct, by(airline type retyr)

collapse (mean) remain_lf gallons eff mkt_value own_emis fitvalue clunkers_pmt (sum) ct, by(type)

gen adj = 0
replace adj = 0.35 if type == "737-400"
replace adj = 0.16 if type == "747-400"
replace adj = 0.25 if type == "757-200"
replace adj = 0.19 if type == "767-300ER" | type == "767-300"
replace adj = 0.18 if type == "A320-200"

gen forgone = adj * own_emis

gen nocpf_cost_per_ton = clunkers_pmt/(adj * own_emis)
gen cpf_cost_per_ton = (1.3 * clunkers_pmt)/(adj * own_emis)

gen agg_p = ct * 1.3 * clunkers_pmt
gen agg_emis = ct * adj * own_emis
collapse (sum) agg_p agg_emis
gen agg_cpf_cost_per_ton = agg_p/agg_emis



